const path = require('path');
var CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')


module.exports = {
    mode: "development",
    entry: {
        "js/content/common/index": "./src/js/content/common/index.tsx", //页面1  content.js
        "js/content/tmall/index": "./src/js/content/tmall/index.ts", //页面2 content.js
        "js/content/jd/index": "./src/js/content/jd/index.ts", //页面3 content.js
        "js/inject/index": "./src/js/inject/index.ts", //页面植入js 共享浏览器环境
        "pages/popup/index": "./src/js/pages/popup/index.ts", //弹出的popup
        "pages/options/index": "./src/js/pages/options/index.ts", //插件选项
        "pages/devtools/index": "./src/js/pages/devtools/index.ts", //调试工具
        "pages/devtoolsPanel/index": "./src/js/pages/devtoolsPanel/index.ts", //调试工具真正打开的页面
        "pages/newtab/index": "./src/js/pages/newtab/index.ts", //新窗口 只可三选一
        "pages/history/index": "./src/js/pages/history/index.ts", //历史页面 只可三选一
        "pages/bookmarks/index": "./src/js/pages/bookmarks/index.ts", //标签页面 只可三选一
        "background": "./src/js/pages/background/index.ts" //背景页
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js',
    },
    // optimization: {
    //     splitChunks: {
    //         chunks: 'all',
    //         cacheGroups: {
    //             defaultVendors: {
    //                 filename: 'js/[name]_bundle.js',
    //             },
    //         },
    //     }
    // },
    module: {
        rules: [{
                test: /\.(ts|tsx)$/,
                exclude: /node_modules/,
                use: [{
                    loader: 'ts-loader',
                }]
            },
            {
                test: /\.(scss|css)$/,
                use: [{
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                    }, {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: function() {
                                    return [
                                        require('autoprefixer')
                                    ];
                                }
                            }
                        }
                    }, {
                        loader: 'sass-loader'
                    }
                ]
            }
        ]
    },
    resolve: {
        extensions: [".ts", ".tsx", ".js"]
    },
    plugins: [
        new CopyWebpackPlugin({
            patterns: [
                { from: "src/manifest.json", to: "" },
                { from: "src/hotreload.js", to: "" },
                {
                    from: "src/img/icon.png",
                    to: "img"
                },
            ],
        }),
        new HtmlWebpackPlugin({
            title: 'popup',
            chunks: ['pages/popup/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/popup.html',
            template: './src/html/popup.html'
        }),
        new HtmlWebpackPlugin({
            title: 'options',
            chunks: ['pages/options/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/options.html',
            template: './src/html/options.html'
        }),
        new HtmlWebpackPlugin({
            title: 'newtab',
            chunks: ['pages/newtab/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/newtab.html',
            template: './src/html/newtab.html'
        }),
        new HtmlWebpackPlugin({
            title: 'bookmarks',
            chunks: ['pages/bookmarks/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/bookmarks.html',
            template: './src/html/bookmarks.html'
        }),
        new HtmlWebpackPlugin({
            title: 'history',
            chunks: ['pages/history/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/history.html',
            template: './src/html/history.html'
        }),
        new HtmlWebpackPlugin({
            title: 'devtools',
            chunks: ['pages/devtools/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/devtools.html',
            template: './src/html/devtools.html'
        }),
        new HtmlWebpackPlugin({
            title: 'devtoolsPanel',
            chunks: ['pages/devtoolsPanel/index'], //添加引入的js,也就是entry中的key
            hash: true,
            minify: {
                collapseWhitespace: true //折叠空白区域 也就是压缩代码
            },
            filename: './html/devtoolsPanel.html',
            template: './src/html/devtoolsPanel.html'
        }),
    ]
}